package com.blog.blogservice.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.blogservice.app.service.CategoryService;
import com.blog.blogservice.app.service.LabelService;
import com.blog.blogservice.domain.entity.Article;
import com.blog.blogservice.domain.entity.Category;
import com.blog.blogservice.domain.entity.Label;
import com.blog.blogservice.infra.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.StringJoiner;

/**
 * @author guanglin.ma
 * @date 2024-03-08 21:01
 */

@SpringBootTest
public class Test {


    @Autowired
    private ArticleMapper articleMapper;


    @org.junit.jupiter.api.Test
    public void test() {
//        articleMapper.selectList(new QueryWrapper<>());
        Article article = new Article();
        article.setArticleCode("1200");
        article.setArticleId(6);
        article.setVersionNum(0);
//        articleMapper.insert(article);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", article.getArticleId());
        articleMapper.update(article, queryWrapper);

    }

    @Autowired
    LabelService labelService;
    @Autowired
    CategoryService categoryService;

    @org.junit.jupiter.api.Test
    public void test1() {
//        QueryWrapper labelWrapper = new QueryWrapper<>();
        String str = "2,10,9,7";
//        labelWrapper.in("label_id", StringUtils.split(str,","));
//        labelWrapper.select("label_id", "label_name");
//        List<Label> labelList = labelService.listQueryWrapper(labelWrapper);
//        if (!CollectionUtils.isEmpty(labelList)){
//            StringJoiner labelIdStringJoiner=new StringJoiner(",");
//            StringJoiner labelNameStringJoiner=new StringJoiner(",");
//            labelList.forEach(val->{
//                labelIdStringJoiner.add(String.valueOf(val.getLabelId()));
//                labelNameStringJoiner.add(val.getLabelName());
//            });
//            System.out.println(labelIdStringJoiner.toString());
//            System.out.println(labelNameStringJoiner.toString());
        String[] str1 = str.split(",");
        QueryWrapper categoryWrapper = new QueryWrapper<>();
        categoryWrapper.in("category_id", str1);
        categoryWrapper.select("category_id", "category_name");
        List<Category> categoryList = categoryService.listQueryWrapper(categoryWrapper);

        if (!CollectionUtils.isEmpty(categoryList)) {
            StringJoiner categoryIdStringJoiner = new StringJoiner(",");
            StringJoiner categoryNameStringJoiner = new StringJoiner(",");
            categoryList.forEach(val -> {
                categoryIdStringJoiner.add(String.valueOf(val.getCategoryId()));
                categoryNameStringJoiner.add(val.getCategoryName());
            });
            System.out.println(categoryIdStringJoiner.toString());
            System.out.println(categoryNameStringJoiner.toString());
        }

    }

    @Autowired
    private RedisTemplate redisTemplate;

    @org.junit.jupiter.api.Test
    public void main() {
//        List<Object> results = redisTemplate.executePipelined((RedisCallback<Object>) connection -> {
//
//            Optional.ofNullable(connection.hLen(BlogConstant.LABEL_LIST.getBytes())).orElse(0L);
//            Optional.ofNullable(connection.hLen(BlogConstant.CATEGORY_LIST.getBytes())).orElse(0L);
//            Optional.ofNullable(connection.hLen(BlogConstant.ARTICLE_LIST.getBytes())).orElse(0L);
//            connection.hGetAll(BlogConstant.LABEL_LIST.getBytes());
//            connection.hGetAll(BlogConstant.CATEGORY_LIST.getBytes());
//
//            return null;
//        });
//
//        ArticleSummary articleSummary = new ArticleSummary();
//        articleSummary.setArticleLabelNum(String.valueOf(results.get(0)));
//        articleSummary.setArticleCategoryNum(String.valueOf(results.get(1)));
//        articleSummary.setArticleNum(String.valueOf(results.get(2)));
//        articleSummary.setLabelSumList(new ArrayList<>());
//        articleSummary.setCategorySumList(new ArrayList<>());
//
//        Map labelMap = (Map) results.get(3);
//        Map categoryMap = (Map) results.get(4);
//
//        if (!MapUtils.isEmpty(labelMap)) {
//            labelMap.forEach((k, v) -> {
//                LabelSumDTO labelSum = new LabelSumDTO();
//                labelSum.setLabelCode(String.valueOf(k));
//                labelSum.setLabelName(String.valueOf(v));
//                articleSummary.getLabelSumList().add(labelSum);
//            });
//        }
//        if (!MapUtils.isEmpty(categoryMap)) {
//            categoryMap.forEach((k, v) -> {
//                CategorySumDTO categorySum = new CategorySumDTO();
//                categorySum.setCategoryCode(String.valueOf(k));
//                categorySum.setCategoryName(String.valueOf(v));
//                articleSummary.getCategorySumList().add(categorySum);
//            });
//        }
//
//        articleSummary.getLabelSumList().forEach(v -> {
//            Long num = redisTemplate.opsForZSet().size(String.format(BlogConstant.ARTICLE_LABEL, v.getLabelCode()));
//            v.setLabelNum(num);
//
//        });
//        articleSummary.getCategorySumList().forEach(v -> {
//            Long num = redisTemplate.opsForZSet().size(String.format(BlogConstant.ARTICLE_CATEGORY, v.getCategoryCode()));
//            v.setCategoryNum(num);
//
//        });
//
//        System.out.println(articleSummary);

//        String key = String.format(BlogConstant.ARTICLE_CATEGORY, "CA1732067836306");
//        System.out.println(redisTemplate.opsForZSet().range("blog:category:CA1732067836306",0,-1));

        Article article = new Article();
        article.setLabelId("10,11");
        QueryWrapper labelWrapper = new QueryWrapper<>();
        labelWrapper.in("label_id", article.getLabelId().split(","));
        labelWrapper.select("label_id", "label_name");
        List<Label> labelList = labelService.listQueryWrapper(labelWrapper);
        if (!CollectionUtils.isEmpty(labelList)) {
            StringJoiner labelIdStringJoiner = new StringJoiner(",");
            StringJoiner labelNameStringJoiner = new StringJoiner(",");
            labelList.forEach(val -> {
                labelIdStringJoiner.add(String.valueOf(val.getLabelId()));
                labelNameStringJoiner.add(val.getLabelName());
            });
            article.setLabelId(labelIdStringJoiner.toString());
            article.setLabelName(labelNameStringJoiner.toString());
        }

        System.out.println(labelList);


    }


}
